package com.autumn.juc.sync;

import org.openjdk.jol.info.ClassLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * 偏向锁批量重偏向测试
 */
public class BiasedLockingTest {
    public static void main(String[] args) throws InterruptedException {
        // 延时产生可偏向对象
        Thread.sleep(5000);
        // 创建一个list，存放锁对象
        List<Object> list = new ArrayList<>();

        // 线程1
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                // 新建锁对象
                Object lock = new Object();
                synchronized (lock) {
                    list.add(lock);
                }
            }
            try {
                // 为了防止 JVM 线程复用，创建完对象后，保持线程thread1状态为存活
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "thread1").start();

        // 睡眠3s，保证线程thread1创建对象完成
        Thread.sleep(3000);
        System.out.println(">>>>>>>>> 打印thread1，list中第20个对象的对象头：");
        System.out.println((ClassLayout.parseInstance(list.get(19)).toPrintable()));

        // 线程2
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                Object obj = list.get(i);
                synchronized (obj) {
                    if (i >= 15 && i <= 21 || i >= 38) {
                        System.out.println(">>>>>>>>>> thread2-第" + (i + 1) + "次加锁执行中" + ClassLayout.parseInstance(obj).toPrintable());
                    }
                }
                if (i == 17 || i == 19) {
                    System.out.println(">>>>>>>>>> thread2-第" + (i + 1) + "次释放锁" + ClassLayout.parseInstance(obj).toPrintable());
                }
            }
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "thread2").start();

        Thread.sleep(5000);

        //线程3
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                Object lock = list.get(i);
                if (i >= 17 && i <= 21 || i >= 35 && i <= 41) {
                    System.out.println(">>>>>>>>>> thread3-第" + (i + 1) + "次准备加锁" + ClassLayout.parseInstance(lock).toPrintable());
                }
                synchronized (lock) {
                    if (i >= 17 && i <= 21 || i >= 35 && i <= 41) {
                        System.out.println(">>>>>>>>>> thread3-第" + (i + 1) + "次加锁执行中" + ClassLayout.parseInstance(lock).toPrintable());
                    }
                }
            }
        }, "thread3").start();

        Thread.sleep(5000);
        System.out.println(">>>>>>>>>> 查看创建的对象：");
        System.out.println(ClassLayout.parseInstance(new Object()).toPrintable());

        LockSupport.park();
    }
}
